package list;

public class Calculator {
    public static void main(String[] args) {
        String expression = "-2-2*6/2";
        ArrayStack2 operStack = new ArrayStack2(10);
        ArrayStack2 numStack = new ArrayStack2(10);
        int index = 0;
        int num1 = 0;
        int num2 = 0;
        int oper = 0;
        int res = 0;
        char ch = ' ';
        String keepNum = "";
        while (true) {
            ch = expression.substring(index, index+1).charAt(0);
            if(operStack.isOper(ch)) {
                if(!operStack.isEmpty()) {
                    if(operStack.priority(ch) <= operStack.priority(operStack.peak())) {
                        if(numStack.curtop() > 1) {
                            num1 = numStack.pop();
                            num2 = numStack.pop();
                            oper = operStack.pop();
                        }else {
                            num1 = numStack.pop();
                            num2 = 0;
                            oper = operStack.pop();
                        }

                        res = numStack.cal(num1, num2, oper);
                        numStack.push(res);
                        operStack.push(ch);
                    }
                    else {
                        operStack.push(ch);
                    }
                }else {
                    operStack.push(ch);
                }
            } else {
                //numStack.push(ch-48);
                keepNum += ch;
                if(index == expression.length() - 1) {
                    numStack.push(Integer.parseInt(keepNum));
                }else {
                if (operStack.isOper(expression.substring(index+1,index+2).charAt(0))) {
                    numStack.push(Integer.parseInt(keepNum));
                    keepNum = "";
                }
            }
            }
            index++;
            if(index >= expression.length()) {
                break;
            }
        }
        while (true) {
            if(operStack.isEmpty()) {
                break;
            }
            num1 = numStack.pop();
            num2 = numStack.pop();
            oper = operStack.pop();
            res = numStack.cal(num1, num2, oper);
            numStack.push(res);
        }
        int res2 = numStack.pop();
        System.out.printf("表达式%s = %d", expression, res2);
    }
}

class ArrayStack2 {
    private int maxSize;
    private int top = -1;
    private int[] stack;

    public ArrayStack2(int maxSize) {
        this.maxSize = maxSize;
        stack = new int[maxSize];
    }

    public boolean isFull() {
        return  top == maxSize - 1;
    }
    public boolean isEmpty() {
        return top == -1;
    }
    public void push(int val) {
        if(isFull()) {
            System.out.println("栈满");
            return;
        }
        top++;
        stack[top] = val;
    }
    public int pop() {
        if(isEmpty()) {
            throw new RuntimeException("栈空");
        }
        int val = stack[top];
        top--;
        return val;
    }

    public void list() {
        if(isEmpty()) {
            System.out.println("栈空");
            return;
        }
        for (int i = top; i >= 0; i--) {
            System.out.printf("%d\n",stack[i]);
        }
    }
    public int priority(int oper) {
        if(oper == '*' || oper == '/') {
            return 1;
        } else if (oper == '+' || oper == '-') {
            return 0;
        }
        else {
            return -1;
        }
    }

    public boolean isOper(char val) {
        return val == '+' || val == '-' || val == '*' || val == '/';
    }

    public int cal(int num1, int num2, int oper) {
        int res = 0;
        switch (oper) {
            case '+':
                res = num1 + num2;
                break;
            case '-':
                res = num2 - num1;
                break;
            case  '*':
                res = num1 * num2;
                break;
            case  '/':
                res = num2 / num1;
                break;
            default:
                break;
        }
        return res;
    }
    public int peak() {
        return stack[top];
    }
    public int curtop() {
        return top;
    }
}
